/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.che.libcommon.widget.viewpager;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;


public class VerticalViewPager extends ViewPager {

  private static final String TAG = "VerticalViewPager";

  public VerticalViewPager(Context context) {
    this(context, null);
  }

  public VerticalViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }


  private OutScroller mOutScroller;

  public void setOutScroller(
      OutScroller outScroller) {
    this.mOutScroller = outScroller;
  }

  /**
   * @return {@code false} since a vertical view pager can never be scrolled horizontally
   */
  @Override
  public boolean canScrollHorizontally(int direction) {
    return false;
  }


  /**
   * @return {@code true} iff a normal view pager would support horizontal scrolling at this time
   */
  @Override
  public boolean canScrollVertically(int direction) {
    return super.canScrollHorizontally(direction);
  }


  private void init() {
    // Make page transit vertical
    setPageTransformer(true, new VerticalPageTransformer());
    // Get rid of the overscroll drawing that happens on the left and right (the ripple)
    setOverScrollMode(View.OVER_SCROLL_NEVER);

    gestureDetector = new GestureDetectorCompat(getContext(),new OnSwipeListener(){
      @Override
      public boolean onSwipe(Direction direction) {
        if (direction == Direction.down){
          return true;
        }
        return super.onSwipe(direction);
      }
    });
  }

  private GestureDetectorCompat gestureDetector;

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean toIntercept = super.onInterceptTouchEvent(flipXY(ev));
    // Return MotionEvent to normal
    flipXY(ev);

    if (!toIntercept && mOutScroller != null){
       boolean isDown = gestureDetector.onTouchEvent(ev);
       if (isDown && !mOutScroller.canScroll()){
         toIntercept = true;
       }
    }

    return toIntercept;
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    final boolean toHandle = super.onTouchEvent(flipXY(ev));
    // Return MotionEvent to normal
    flipXY(ev);
    //Log.e(TAG, "onTouchEvent --> "+toHandle);
    return toHandle;
  }

  private MotionEvent flipXY(MotionEvent ev) {
    final float width = getWidth();
    final float height = getHeight();
    final float x = (ev.getY() / height) * width;
    final float y = (ev.getX() / width) * height;
    ev.setLocation(x, y);
    return ev;
  }



  public interface OutScroller{
    boolean canScroll();
  }

  private static final class VerticalPageTransformer implements ViewPager.PageTransformer {

    @Override
    public void transformPage(View view, float position) {
      final int pageWidth = view.getWidth();
      final int pageHeight = view.getHeight();
      if (position < -1) {
        // This page is way off-screen to the left.
        view.setAlpha(0);
      } else if (position <= 1) {
        view.setAlpha(1);
        // Counteract the default slide transition
        view.setTranslationX(pageWidth * -position);
        // set Y position to swipe in from top
        float yPosition = position * pageHeight;
        view.setTranslationY(yPosition);
      } else {
        // This page is way off-screen to the right.
        view.setAlpha(0);
      }
    }
  }
}